// 定义数据库操作类

//引入db配置
const db = require('../config/mysql_sequelize')

//引入sequelize对象
const Sequelize = db.sequelize

//引入数据表模型
const user = Sequelize.import(__dirname + '/user.js') //__dirname（动态获取）可以用来获取当前文件模块所属目录的绝对路径
const role = Sequelize.import(__dirname + '/role.js')
const user_role = Sequelize.import(__dirname + '/user_role.js')
const association = Sequelize.import(__dirname + '/association.js')

//用户与角色多对多  foreignKey让你可以设置through关系中的源模型，而otherKey让你可以through关系中的目标模型。
user.belongsToMany(role, {
  through: 'user_role',
  foreignKey: 'account',
  sourceKey: 'account'
});
role.belongsToMany(user, {
  through: 'user_role',
  foreignKey: 'role_id',
  sourceKey: 'id'
});

//用户与活动多对多
user.belongsToMany(association, { through: 'user_association', foreignKey:'account', sourceKey:'account'});
association.belongsToMany(user, { through: 'user_association', foreignKey: 'association_id', sourceKey:'id'});


module.exports = {
  user,
  role,
  user_role,
  association
}